<?php
namespace App\Services\Wxxcx;

use Illuminate\Support\Facades\DB;

class ScanService extends BaseService
{
    /**
     * 二维码
     */

    /**
     * 判断码有没有
     * @param string $code 码值
     * @return array
     */
    public function isExist($code)
    {
        $qrOne = (array) $this->dbConn($this->dbQr)
            ->where('code', $code)
            ->first();
        if (empty($qrOne)) {
            return array('code' => -2, 'msg' => '非法码');
        }
        if ($qrOne['tmp_id']) {
            return array('code' => -2, 'msg' => '码已被使用');
        }
        return array('code' => 0, 'msg' => 'success', 'data' => $qrOne);
    }

    /**
     * 详情ID
     * @param $type 类型
     * @param string $code 码值
     */
    public function getOne($code)
    {
        $qrOne = (array) $this->dbConn($this->dbQr)
            ->where('code', $code)
            ->first();
        if (empty($qrOne)) {
            return array('code' => -2, 'msg' => '非法码');
        }
        if ($qrOne['view'] == '') {
            return array('code' => -2, 'msg' => '空码');
        }
        $type = $qrOne['view_type'];
        if ($type == 1) {
            $one = (array) $this->dbConn($this->dbTmp)
                ->where('del', 1)
                ->where('code', $code)
                ->first();
        } else if ($type == 2) {
            $one = (array) $this->dbConn($this->dbShop)
                ->where('del', 1)
                ->where('qrcode', $code)
                ->first();
        } else if ($type == 3) {
            $one = (array) $this->dbConn($this->dbGoods)
                ->where('del', 1)
                ->where('qrcode', $code)
                ->first();
        } else {
            return array('code' => -2, 'msg' => '类型错误');
        }
        if (empty($one)) {
            return array('code' => -2, 'msg' => '没有详情');
        }
        $arys = [
            'type' => $type,
            'id' => $one['id'],
        ];
        return array('code' => 0, 'msg' => 'success', 'data' => $arys);
    }

    /**
     * 查编号
     * @param string $serial 编号
     */
    public function getOneBySerial($serial)
    {
        $qrOne = (array) $this->dbConn($this->dbQr)
            ->where('show_code', $serial)
            ->first();
        if (empty($qrOne)) {
            return $this->toback(-2, '非法码');
        }
        if (!$qrOne['tmp_id']) {
            return $this->toback( -2, '空码');
        }
        $code = $qrOne['code'];
        $tmpOne = (array) $this->dbConn($this->dbTmp)
            ->where('del', 1)
            ->where('code', $code)
            ->first();
        if (empty($tmpOne)) {
            return $this->toback(-2, '没有记录');
        }
        $sceneOne = (array) $this->dbConn($this->dbScene)
            ->where('id', $tmpOne['scene_id'])
            ->first();
        if (empty($sceneOne)) {
            return $this->toback(-2, '场景错误');
        }
        $visit_total = $this->dbConn($this->dbQrlog)
            ->where('code', $code)
            ->count();
        $qrlog = (array) $this->dbConn($this->dbQrlog)
            ->where('code', $code)
            ->orderBy('id', 'desc')
            ->first();
        $lasttime = empty($qrlog) ? '-' : $qrlog['addtime'];
        $arys = [
            'scene_name' => $sceneOne['title'],
            'code' => $qrOne['code'],
            'title' => $tmpOne['title'],
            'visit_total' => $visit_total,
            'lasttime' => $lasttime,
        ];
        return array('code' => 0, 'msg' => 'success', 'data' => $arys);
    }
}